package client;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.File;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import common.Commands;
import common.SendBin;
import common.Lib;
import common.SendUUID;

public class UploadDialog {

	private String address;
    private int port;
    
	private JFrame frame;
	private JTextField message, author, fileName, parameters;
	
	private Client client;
	private JTable table;
	
	public UploadDialog(String address, int port, Client client, JTable table)
    {
        this.address = address;
        this.port = port;
        this.client = client;
        this.table = table;
    }
		
	public void createAddShowGUI()
	{
		JButton button = null;
		JPanel panel = null;
		JLabel label = null;
		
		frame = new JFrame("Upload task dialog");
		frame.setSize(500, 175);
		frame.setLocation(330, 330);
		frame.setLayout(new BorderLayout());
		
		{
			panel = new JPanel();
			panel.setLayout(new GridLayout(4,1));
			
			label = new JLabel("Message");
			panel.add(label);
			
			label = new JLabel("Author");
			panel.add(label);
			
			label = new JLabel("File Name");
			panel.add(label);
			
			label = new JLabel("Parameters");
			panel.add(label);
			
			frame.add(panel, BorderLayout.WEST);
		}
		
		{
			panel = new JPanel();
			panel.setLayout(new GridLayout(4,1));
			
			message = new JTextField(20);
			panel.add(message);
			
			author = new JTextField(20);
			panel.add(author);
			
			fileName = new JTextField(20);
			fileName.setText("C:\\BitField.exe");
			panel.add(fileName);
			
			parameters = new JTextField(20);
			panel.add(parameters);
			
			frame.add(panel, BorderLayout.CENTER);
		}
		
		{
			panel = new JPanel();
			panel.setLayout(new GridLayout(2,1));
			
			JPanel panel1;
			
			{
				panel1 = new JPanel();
				
				panel1.setLayout(new GridLayout(1,4));
				
				label = new JLabel("");
				panel1.add(label);
				label = new JLabel("");
				panel1.add(label);
				label = new JLabel("");
				panel1.add(label);
				
				button = new JButton("Browse...");
		        button.addActionListener(new ActionListener() {	        	 
		            public void actionPerformed(ActionEvent e)
		            {
		            	fileName.setText(Browse());
		            }
		        });
				panel1.add(button);
				
				panel.add(panel1);
			}
			
			{
				panel1 = new JPanel();
				panel1.setLayout(new GridLayout(1,3));
				
				label = new JLabel("");
				panel1.add(label);
				
				button = new JButton("Upload");
		        button.addActionListener(new ActionListener() {	        	 
		            public void actionPerformed(ActionEvent e)
		            {
		            	Upload();
		            }
		        });
				panel1.add(button);
				
				label = new JLabel("");
				panel1.add(label);
				
				panel.add(panel1);
			}
			
			frame.add(panel, BorderLayout.PAGE_END);
		}		
		frame.setVisible(true);
	}
	
	private String Browse()
	{
	    JFileChooser fileopen = new JFileChooser();
	    FileFilter filter = new FileNameExtensionFilter("exe files", "exe");
	    fileopen.addChoosableFileFilter(filter);

	    int ret = fileopen.showDialog(null, "Open file");

	    if (ret == JFileChooser.APPROVE_OPTION) {
	      File file = fileopen.getSelectedFile();
	      //System.out.println(file);
	      return file.getAbsolutePath();
	    }
	    return "";
	}
	
	private void Upload()
	{
		frame.setVisible(false);
		byte[] bytes = null;
		String file = fileName.getText();
		bytes = Lib.readBinFile(file);
		/*
		System.out.println(bytes.length);
		Lib.writeBinFile(bytes, "D:\\P.exe");
		*/
		SendBin bin = new SendBin();
		
		bin.message = message.getText();
		bin.author = author.getText();
		bin.parameters = parameters.getText();
		bin.binFile = bytes;
		bin.fileName = file.substring(file.lastIndexOf('\\')+1);
		System.out.println(bin.fileName);
				
		SendingClientThread sendingThread = new SendingClientThread(address, port, Commands.UploadRequest, bin, table);
		sendingThread.start();
		/*
		try {
			sendingThread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.UpdateTable();
		*/
		
		/*
		Socket cs;
		OutputStream os;
		InputStream is;
		
		try {
			cs = new Socket(address, port);
	        is = cs.getInputStream();
			os = cs.getOutputStream();
			
	        os.write(Commands.UUIDRequest);
	        os.flush();
	        ObjectInputStream ois = new ObjectInputStream(is);
	        SendUUID sID;
			sID = (SendUUID) ois.readObject();
			System.out.println(sID.idUser);
			
			os.write(Commands.UploadRequest);
	        os.flush();
	        	        
	        ObjectOutputStream oos = new ObjectOutputStream(os);
	        oos.writeObject(bin);
	        oos.flush();
	        
            os.write(Commands.UnlinkRequest);
            os.flush();
            cs.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}
}